
//
//  HZZ_SBDiscoverVC.m
//  HZZ_Sb
//
//  Created by tgjr-Hzz on 2018/1/23.
//  Copyright © 2018年 Hzz. All rights reserved.
//

#import "HZZ_SBDiscoverVC.h"
#import "PGGTableAndCollecton.h"

#import "HZZDiscoverManger.h"

#import "UIViewController+MMDrawerController.h"



@interface HZZ_SBDiscoverVC ()<UITableViewDataSource ,UITableViewDelegate>

@property (nonatomic ,strong) UIView *navView;

@property (nonatomic ,strong) UITableView *tableView;

@property (nonatomic ,strong) NSMutableArray *dataArray;

@property (nonatomic, strong) HZZDiscoverManger *discoverManger;

@end

@implementation HZZ_SBDiscoverVC

-(void)viewWillAppear:(BOOL)animated
{
    [self.navigationController setNavigationBarHidden:YES animated:animated];
    [super viewWillAppear:animated];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:animated];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.view addSubview:self.tableView];
    [self setUpRefreshView];
    [self setDownRefreshView];
    
    [self.view addSubview:self.navView];

    
    [self fetchLeftRecommendedType:@"saint"];
    [self fetchContentImagesRid:@"13"];
    
   
}

#pragma mark - Button Handlers
-(void)leftDrawerButtonPress:(id)sender{
}

- (void)leftAction:(UIButton *)btn
{
    [self.mm_drawerController toggleDrawerSide:MMDrawerSideLeft animated:YES completion:nil];

}

- (void)rightAction:(UIButton *)btn
{
    PGGTableAndCollecton *tableAndC = [PGGTableAndCollecton new];
    [self.navigationController pushViewController:tableAndC animated:YES];
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 100;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
//    return self.dataArray.count;
    return 10;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *cellStr = @"cellStr";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellStr];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellStr];
    }
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
}

-(UIView *)navView
{
    if (!_navView) {
        _navView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_W, NaviH)];
        _navView.backgroundColor = [UIColor whiteColor];
        
        UILabel *titleLabel=[[UILabel alloc] initWithFrame:CGRectMake( 40 ,NaviH - 40 ,SCREEN_W - 80 , 40)];
        titleLabel.text = @"首页";
        titleLabel.font= fontWithPingfang(17);
        titleLabel.textAlignment =NSTextAlignmentCenter;
        [_navView addSubview:titleLabel];
        
        UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [leftButton setImage:[UIImage imageNamed:@"tabbar_icon_message_s"] forState:UIControlStateNormal];
        //    [leftButton setImage:[UIImage imageNamed:@"nav_select_search"] forState:UIControlStateHighlighted];
        [leftButton addTarget:self action:@selector(leftAction:) forControlEvents:UIControlEventTouchUpInside];
        leftButton.imageView.contentMode = UIViewContentModeScaleAspectFit;
        leftButton.frame = CGRectMake(0, NaviH - 44, 44, 44);
        leftButton.imageEdgeInsets = UIEdgeInsetsMake(0, 5, 0, 0);
        [_navView addSubview:leftButton];
        
        
        UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [rightButton setImage:[UIImage imageNamed:@"tabbar_icon_message_s"] forState:UIControlStateNormal];
        //    [rightButton setImage:[UIImage imageNamed:@"nav_select_rank"] forState:UIControlStateHighlighted];
        [rightButton addTarget:self action:@selector(rightAction:) forControlEvents:UIControlEventTouchUpInside];
        rightButton.imageView.contentMode = UIViewContentModeScaleAspectFit;
        rightButton.frame = CGRectMake(SCREEN_W - 44, NaviH - 44, 30, 44);
        rightButton.imageEdgeInsets = UIEdgeInsetsMake(0, -5, 0, 0);
        [_navView addSubview:rightButton];
        
        CALayer *link = [CALayer layer];
        link.frame = CGRectMake(0, NaviH - 1/[UIScreen mainScreen].scale, SCREEN_W, 1/[UIScreen mainScreen].scale);
        link.backgroundColor = UIColorFromHex(0xDCDCDC).CGColor;
        [_navView.layer addSublayer:link];
        
    }
    return _navView;
}


-(UITableView *)tableView{
    if(!_tableView){
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, NaviH, SCREEN_W, SCREEN_H - NaviH - TabbarH) style:UITableViewStylePlain];
        _tableView.backgroundColor = UIColorFromHex(0xFFFFFF);
        _tableView.delegate = self;
        _tableView.dataSource = self;
//        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        UIView *vv = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_W, 0)];
        vv.backgroundColor = UIColorFromHex(0xF5F5F5);
        _tableView.tableHeaderView = vv;
        _tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
        
        if (@available(iOS 11.0, *)){
            _tableView.estimatedRowHeight = 0;
            _tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        }
    }
    return _tableView;
}

- (void)setUpRefreshView {
    __weak typeof(self)weakSelf = self;
    WYWRefreshGifHeader *header = [WYWRefreshGifHeader headerWithRefreshingBlock:^{
        
        [weakSelf.tableView.mj_footer endRefreshing];
    }];
    header.lastUpdatedTimeLabel.hidden = YES;
    header.stateLabel.hidden = YES;
    self.tableView.mj_header = header;
}

- (void)setDownRefreshView {
    __weak typeof(self)weakSelf = self;
    self.tableView.mj_footer = [WYWRefreshGifFooter footerWithRefreshingBlock:^{
        
        [weakSelf.tableView.mj_header endRefreshing];
    }];
    
}


-(HZZDiscoverManger *)discoverManger
{
    if (!_discoverManger) {
        _discoverManger = [[HZZDiscoverManger alloc] init];
    }
    return _discoverManger;
}

-(void)fetchLeftRecommendedType:(NSString *)type
{
    [[self.discoverManger fetchLeftRecommendedType:type] subscribeNext:^(NSMutableArray * dataArray) {
        
        
    } error:^(NSError *error) {
        [WYWAlertView showErrorAlertQuiet:error];
    } completed:^{
         
    }];
    
}


-(void)fetchContentImagesRid:(NSString *)rid
{
    [[self.discoverManger fetchContentImagesRid:rid] subscribeNext:^(NSMutableArray * dataArray) {
        
        NSArray *images = ((HZZFigureBedModel *)[dataArray firstObject]).imgs;
        HZZImageModel *imageModel = [images firstObject];
        NSString *path = [UIImage getImagePath:imageModel];
        NSLog(@"%@",path);
        
        
    } error:^(NSError *error) {
        [WYWAlertView showErrorAlertQuiet:error];
    } completed:^{
        
    }];
    
}
@end
